import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
  ViewEncapsulation,
} from "@angular/core";
import { ModalDirective } from "ngx-bootstrap";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  WlReleaseStockBillListDto,
  MaterialIntoServiceProxy,
  WlReleaseStockBillEditDto,
  ComboboxItemDto,
  IntoWarehouseInput,
  LocationServiceProxy,
  LocationDto,
} from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";

@Component({
  selector: "putIntoModal",
  templateUrl: "./put-into-modal.component.html",
  styleUrls: ["./put-into-modal.component.less"],
  encapsulation: ViewEncapsulation.None,
})
export class PutIntoModalComponent extends AppComponentBase {
  @ViewChild("actionModal") modal: ModalDirective;

  @ViewChild("areaCombobox") areaCombobox: ElementRef;
  @ViewChild("shelfCombobox") shelfCombobox: ElementRef;
  @ViewChild("positionCombobox") positionCombobox: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  materialType:string="";
  materialLY:string="";

  activeIndex = 0;

  releaseStockBill = new WlReleaseStockBillEditDto();

  areas = new Array<LocationDto>();
  shelfs = new Array<LocationDto>();
  positions = new Array<LocationDto>();

  hasLoadLocations = false;

  areaId = 0;
  shelfId = 0;
  positionId = 0;

  items = [{ label: "数据校验" }, { label: "位置选择" }];

  constructor(
    injector: Injector,
    private _warehouseIntoServiceProxy: MaterialIntoServiceProxy,
    private _locationServiceProxy: LocationServiceProxy
  ) {
    super(injector);
  }

  show(bill: WlReleaseStockBillListDto): void {
    this.activeIndex = 0;

    this.areaId = 0;
    this.shelfId = 0;
    this.positionId = 0;

    this.active = true;

    this.hasLoadLocations = false;

    this._warehouseIntoServiceProxy
      .getReleaseStockBillForEdit(bill.id)
      .subscribe((result) => {
        this.releaseStockBill = result;
        setTimeout(() => {
          $(this.areaCombobox.nativeElement).selectpicker("refresh");
          $(this.shelfCombobox.nativeElement).selectpicker("refresh");
          $(this.positionCombobox.nativeElement).selectpicker("refresh");
        }, 0);

        this.modal.show();
      });
  }

  onNextBtnClick() {
    this.activeIndex = 1;

    if (!this.hasLoadLocations) {
      this._locationServiceProxy.getAreas().subscribe((result) => {
        this.hasLoadLocations = true;
        this.areas = result.items;
        setTimeout(() => {
          $(this.areaCombobox.nativeElement).selectpicker("refresh");
        }, 0);
      });
    }
  }

  loadShelfs() {
    this.shelfId = 0;
    this.positionId = 0;

    this._locationServiceProxy
      .getShelvesByArea(this.areaId)
      .subscribe((result) => {
        this.shelfs = result.items;
        setTimeout(() => {
          $(this.shelfCombobox.nativeElement).selectpicker("refresh");
        }, 0);
      });
  }

  loadPositions() {
    this.positionId = 0;

    this._locationServiceProxy
      .getPositionsByShelf(this.shelfId)
      .subscribe((result) => {
        this.positions = result.items;
        setTimeout(() => {
          $(this.positionCombobox.nativeElement).selectpicker("refresh");
        }, 0);
      });
  }
  getTypeChange(val){
    this.materialType=val;
    console.log(val);
  }
  getLYChange(val){
    this.materialLY=val;
    console.log(val);
  }
  save(): void {
    this.saving = true;
    let input = new IntoWarehouseInput();
    input.wlReleaseStockBill = this.releaseStockBill;
    input.wlReleaseStockBill.materialType=this.materialType;
    input.wlReleaseStockBill.materialLY=this.materialLY;
    input.positionId = this.positionId;
    console.log(input);
    this._warehouseIntoServiceProxy
      .intoWarehouse(input)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe((result) => {
        console.log(input);

        this.notify.info("入库成功");
        this.close();
        this.modalSave.emit();
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
